/*
 * timing-posix.c
 *
 *  Created on: Jun 7, 2012
 *      Author: burek
 */

#ifndef WIN32

#include "timing.h"
#include <stdlib.h>
#include <sys/time.h>
#include <time.h>

#ifndef CLOCK_MONOTONIC_RAW
#define CLOCK_MONOTONIC_RAW 4
#endif

#ifndef CLOCK_MONOTONIC
#define CLOCK_MONOTONIC 1
#endif

struct timespec start;

void cu_initTiming()
{
	clock_gettime(CLOCK_MONOTONIC, &start);
}

CUSecTime cu_getSecTime()
{
	struct timeval ctime;
	CUSecTime result;
	struct tm * timeinfo;

	gettimeofday(&ctime, NULL);

	timeinfo = localtime((time_t *)&ctime.tv_sec);

	result.seconds = timeinfo->tm_sec;
	result.microSeconds = ctime.tv_usec;
	return result;
}

unsigned int cu_getTickCount()
{
	unsigned int ticks;
	struct timespec ts;
	clock_gettime(CLOCK_MONOTONIC, &ts);
	ticks=(ts.tv_sec - start.tv_sec) * 1000 + (ts.tv_nsec - start.tv_nsec) / 1000000;
	return ticks;
}

#endif
